<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

	<xsl:import href="toXMIFunction.xsl"/>
	<xsl:import href="toXMI.xsl" />
	
	<xsl:template match="/">
		<xsl:apply-imports />
	</xsl:template>
	
	
<!-- ****************** noeud 'Generalization' ****************** -->
	<xsl:template match="Generalization" >
		<xsl:apply-templates select="." mode="Generalization" />
	</xsl:template>
	
<!-- ****************** template attribute 'CLIENTCLASSNAME' ****************** -->	
	<xsl:template match="Generalization" mode="Generalization.child_idRef">
		<xsl:value-of select="Generalization.child//@xmi.idref"/>
	</xsl:template>
	<xsl:template match="Generalization" mode="Generalization.parent_idRef">
		<xsl:value-of select="Generalization.parent//@xmi.idref"/>
	</xsl:template>
	
	<!--<xsl:template name="AttributeClientClassName">
		<xsl:param name="node" />
		
		<xsl:attribute name="clientClassName">
			<xsl:choose>
				<xsl:when test="$node/self::Generalization">
					<xsl:apply-templates select="$node/Generalization.child/*" mode="GetClassName" />
				</xsl:when>
				<xsl:when test="$node/self::Abstraction">
					<xsl:apply-templates select="$node/Dependency.client/*" mode="GetClassName" />
				</xsl:when>
				<xsl:when test="$node/self::Association  | $node/self::AssociationClass">
					<xsl:variable name="no" ><xsl:apply-templates select="Association.connection" mode="GetAssociationClient"/></xsl:variable>
					<xsl:apply-templates select="$node/Association.connection/AssociationEnd[position()=$no]/AssociationEnd.participant/*" mode="GetClassName" />
				</xsl:when>
				
			</xsl:choose>			
		</xsl:attribute>
	</xsl:template>-->
	
	
<!-- ****************** template attribute 'SUPPLIERCLASSNAME' ****************** -->
<!--	<xsl:template name="AttributeSupplierClassName">
		<xsl:param name="node" />
		
		<xsl:attribute name="supplierClassName">
			<xsl:choose>
				<xsl:when test="$node/self::Generalization">
					<xsl:apply-templates select="$node/Generalization.parent/*" mode="GetClassName" />
				</xsl:when>
				<xsl:when test="$node/self::Abstraction">
					<xsl:apply-templates select="$node/Dependency.supplier/*" mode="GetClassName" />
				</xsl:when>
				<xsl:when test="$node/self::Association | $node/self::AssociationClass">
					<xsl:variable name="no" ><xsl:apply-templates select="Association.connection" mode="GetAssociationSupplier"/></xsl:variable>
					<xsl:apply-templates select="$node/Association.connection/AssociationEnd[position()=$no]/AssociationEnd.participant/*" mode="GetClassName" />
				</xsl:when>
			</xsl:choose>
		</xsl:attribute>
	</xsl:template>-->

<!-- ****************** fonction 'GetClassName' ****************** -->	
	<xsl:template match="Class | Interface" mode="GetClassName">
		<xsl:variable name="idRef" select="@xmi.idref" />
		<xsl:value-of select="//*[@xmi.id=$idRef]/@name" />
	</xsl:template>
	
<!-- ****************** fonction 'GetRoleName' ****************** -->
	<xsl:template match="Association.connection" mode="GetRoleName">
		<xsl:param name="role"/>
		
		<xsl:variable name="roleClient"><xsl:apply-templates select="." mode="GetAssociationClient" /></xsl:variable>
		<xsl:variable name="roleSupplier"><xsl:apply-templates select="." mode="GetAssociationSupplier" /></xsl:variable>
				
		<xsl:choose>
			<xsl:when test="$role=$roleClient and (AssociationEnd[position()=$role]/@name = 'undefined' or not(AssociationEnd[position()=$role]/@name))">
				<xsl:apply-templates select="AssociationEnd[position()=$role]/AssociationEnd.participant/*" mode="GetClassName"/>
				<xsl:value-of select="AssociationEnd[position()=$roleSupplier]/@name" />
				<xsl:text>Parent</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="AssociationEnd[position()=$role]/@name"/>
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>
	
<!-- ****************** template attribute 'TYPE' ****************** -->	
	<xsl:template name="AttributeType">
		<xsl:param name="node"/>
		<xsl:attribute name="type">
			<xsl:choose>
				<xsl:when test="$node/self::Operation">
					<xsl:variable name="id" select="$node/BehavioralFeature.parameter/Parameter[@kind='return']/@type" />
					<xsl:apply-templates select="//DataType[@xmi.id=$id]" mode="GetType"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="id" select="$node/StructuralFeature.type/DataType/@xmi.idref | $node/TypedElement.type/DataType/@xmi.idref" />
					<xsl:apply-templates select="//DataType[@xmi.id=$id]" mode="GetType"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
	</xsl:template>
		
<!-- ****************** fonction 'GetType' ****************** -->	
	<xsl:template match="DataType" mode="GetType">	
		<xsl:choose>
			<!-- si le type n'est pas défini -->
			<xsl:when test="@name = '' or @name= '&amp;lt;undefined&amp;gt;'">
				<xsl:text>void</xsl:text>
			</xsl:when>
			<!-- si le type est défini -->
			<xsl:otherwise>
				<xsl:value-of select="@name"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
